<template>
  <div class="app-container">
    <el-dialog title="人员信息" :visible.sync="dialogVisible" width="80%" :before-close="handleClose">
    <el-row>
      <el-col :span="24">
        <el-form :model="queryParams" ref="queryForm" size="mini" :inline="true" v-show="showSearch" label-width="85px" style="margin-left:-35px">
          <el-form-item label="姓名" prop="name">
            <el-input v-model="queryParams.name" placeholder="请输入姓名" clearable @keyup.enter.native="handleQuery"
                      width="150px" />
          </el-form-item>
          <el-form-item label="标签" prop="labelPerson">
            <el-select v-model="queryParams.labelPerson" placeholder="请选择标签" style="width:150px">
              <el-option
                v-for="dict in dict.type.label_person"
                :key="dict.value"
                :label="dict.label"
                :value="dict.value"
              ></el-option>
            </el-select >
          </el-form-item>
          <el-form-item label="企业名称" prop="enterpriseName" v-if="enterpriseId === null">
            <el-input v-model="queryParams.enterpriseName" placeholder="请输入企业名称" clearable
                      @keyup.enter.native="handleQuery" />
          </el-form-item>
          <el-form-item>
          <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
          <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
          <el-button type="primary" icon="el-icon-plus" size="mini" @click="handleConfirm">确定</el-button>
        </el-form-item>
        </el-form>
        <el-table :row-style="{ height: '30px' }" :cell-style="{ padding: '0px' }" :height="500" border
                  :header-cell-style="{'text-align':'center'}"   v-loading="loading" :data="personList" @selection-change="handleSelectionChange" highlight-current-row
                  @row-click="handleRowClick" ref="singleTable">
          <el-table-column type="radio" width="40" align="center" v-if="showClose === false" />
          <el-table-column label="序" type="index" :index="indexMethod" width="35"/>
          <el-table-column label="建档时间" align="center" prop="filingTime" :width="85">
            <template slot-scope="scope">
              <span>{{ parseTime(scope.row.filingTime, '{y}-{m}-{d}') }}</span>
            </template>
          </el-table-column>
          <el-table-column label="建档人" align="center" prop="userName" :show-overflow-tooltip="true" width="70" />
          <el-table-column label="姓名" align="center" prop="name" :show-overflow-tooltip="true" width="60"/>
          <el-table-column label="联系方式" align="center" prop="phone" :show-overflow-tooltip="true" width="110"/>
          <el-table-column label="性别" align="center" prop="sex" :show-overflow-tooltip="true" width="60" >
            <template slot-scope="scope">
              <dict-tag :options="dict.type.sys_user_sex" :value="scope.row.sex" />
            </template>
          </el-table-column>
          <el-table-column label="从业企业" align="left" prop="enterpriseName" :show-overflow-tooltip="true" width="195"/>
          <el-table-column label="职务" align="center" prop="position" :show-overflow-tooltip="true" width="60"/>
          <el-table-column label="状态" align="center" prop="status"  width="50">
            <template slot-scope="scope">
              <dict-tag :options="dict.type.enter_person" :value="scope.row.status" />
            </template>
          </el-table-column>
          <el-table-column label="最新联系时间" align="center" prop="contactTime" :width="100" :show-overflow-tooltip="true">
            <template slot-scope="scope">
              <span>{{ parseTime(scope.row.contactTime, '{y}-{m}-{d}') }}</span>
            </template>
          </el-table-column>
          <el-table-column label="最新联系内容" align="center" prop="contactContent" :width="100" :show-overflow-tooltip="true">
            <template slot-scope="scope">
              <span>{{ parseTime(scope.row.contactContent, '{y}-{m}-{d}') }}</span>
            </template>
          </el-table-column>
          <el-table-column label="参训次数" align="center" prop="activityCount" :show-overflow-tooltip="true" width="70"/>
          <el-table-column label="标签" align="center" prop="labelPerson"  >
            <template slot-scope="scope">
              <dict-tag :options="dict.type.contact_tag" :value="scope.row.labelPerson" />
            </template>
          </el-table-column>
        </el-table>
        <pagination v-show="total > 0" :total="total" :page.sync="queryParams.pageNum"
                    :limit.sync="queryParams.pageSize" @pagination="getList" />
      </el-col>
    </el-row>
    </el-dialog>
  </div>
</template>

<script>
import { listPerson, getPerson, delPerson, addPerson, updatePerson } from "@/api/enterprise/person";
import BusinessInfo from "../info/index.vue";
import MainType from "@/components/MainType/index"
import FileView from "@/components/FileView/index"
import Treeselect from "@riophae/vue-treeselect";
import "@riophae/vue-treeselect/dist/vue-treeselect.css";
import UploadFile from "../../upload/index.vue";
import ChangeEnter from "./enterprise.vue";
import Contract from "./contract.vue";
import PersonRecords from "./records.vue";
import TableOperationButtons from "@/components/TableOperationButtons";

export default {
  components: { PersonRecords, Contract, ChangeEnter, UploadFile, Treeselect, BusinessInfo, FileView, MainType, TableOperationButtons },
  dicts: ['sys_normal_disable', 'sys_user_sex', 'enter_person','contact_tag'],
  name: "enterpriseUser",
  props: {
    enterpriseId: {
      type: String,
      default: null
    },
    enterpriseType: {  //传入公海、电销、还是客户
      type: String,
      default: null
    },
    showClose: {
      type: Boolean,
      default: false
    },
    enterpriseName: {
      type: String,
      default: null
    },
    //提供给别的模块调用人员管理使用，企业管理现任企业选择，只能是当前企业
    isOnlyRead: {
      type: Boolean,
      default: false,
    },
    isOpen: {
      type: Boolean,
      default: false
    },
    isEnterprise: {
      type: Boolean,
      default: false,
    },
    showBtn: {
      type: Boolean,
      default: true
    },
    opt:{
      type:String,
      default:''
    }
  },
  data() {
    var checkPhone = (rule, value, callback) => { // 手机号验证
      if (!value) {
        return callback(new Error('手机号不能为空'));
      } else {
        const reg = /^1[3456789]\d{9}$/
        if (reg.test(value)) {
          callback();
        } else {
          return callback(new Error('请输入正确的手机号'));
        }
      }
    };
    return {
      //主分类
      typeCode: 'person',
      dialogVisible:false,
      honorShow: 0,
      // 遮罩层
      loading: true,
      //只读
      readonly: false,
      // 选中数组
      ids: [],
      ImgDig: false,
      //图片的标志位
      show: false,
      isOnlyEnter: false,
      activeName: 'first',//tab页
      // 非单个禁用
      single: true,
      //附件对话框
      openFileList: false,
      uploadId: null,
      dialogImageUrl: null,
      // 非多个禁用
      multiple: true,
      openUser: false,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      typeSelect: 0,
      // 企业人员管理表格数据
      personList: [],
      //详情状态
      ReadOnly: false,

      // 弹出层标题
      title: "",
      businessCode: 'enterprise_person',
      // 是否显示弹出层
      open: false,
      //新增或编辑
      addSign: 0,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 15,
        enterpriseId: null,
        name: null,
        position: null,
        enterpriseName: null,
        labelPerson:null
      },
      //主分类
      options: null,
      // 表单参数
      form: {
        enterpriseName: null,
      },
      // 表单校验
      rules: {
        enterpriseName: [
          { required: true, message: '企业不能为空' },
          {
            validator: (rule, value, callback) => {
              if (!value && this.form.enterpriseName) {
                callback(new Error('企业不能为空'));
              } else {
                callback();
              }
            },
            trigger: 'change'
          }
        ],
        name: [
          { required: true, message: '请输入姓名', trigger: 'blur' }
        ],
        phone: [
          { required: true, message: '请输入电话号码', trigger: 'blur' },
          { validator: checkPhone, trigger: 'blur' }
        ],
        sex: [
          { required: true, message: '请选择性别', trigger: 'change' },
        ],
      }
    };
  },
  created() {
    this.getList();
    this.listType();
    this.dialogVisible=this.isEnterprise;
  },
  watch: {
    isEnterprise: {
      handler(val) {
        this.dialogVisible = val;
      },
      immediate: true
    }
  },
  methods: {
    /** 查询企业人员管理列表 */
    getList() {
      this.loading = true;
      this.queryParams.enterpriseId = this.enterpriseId;
      listPerson(this.queryParams).then(response => {
        this.personList = response.rows;
        console.log(this.personList)
        this.total = response.total;
        this.loading = false;
      });
    },
    handleClose(){
      this.dialogVisible = false;
      this.$emit('select-user');
    },
    // 取消按钮
    cancel() {
      this.show = false;
      this.open = false;
      this.typeSelect = 0;
      this.activeName = 'first';
      this.reset();
    },
    personClose() {
      this.cancel();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        enterpriseId: null,
        name: null,
        phone: null,
        position: null,
        sex: null,
        remark: null,
        operateId: null,
        departmentId: null,
        departmentName: null,
        organizationId: null,
        organizationName: null,
        creationTime: null,
        operateTime: null,
        delFlag: null,
        filingTime: null,
        supervisorScope: null,
        title: null,
        education: null,
        nativePlace: null,
        birthday: null,
        entryTime: null,
        createId: null,
        status: null,
        userName: null,
        fileUrl: null,
        enterpriseName: null,
        uploadFile: null,
        labelPerson: null,
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.queryParams.name = null;
      this.queryParams.education = null;
      this.queryParams.position = null;
      this.queryParams.enterpriseName = null;
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      if (selection.length > 0) {
        this.ids = [selection[0].id];
        this.form = { ...selection[0] };
        this.single = selection.length !== 1;
        this.multiple = !selection.length;
      } else {
        this.ids = [];
        this.form = {
          enterpriseName: null,
        };
        this.single = true;
        this.multiple = true;
      }
    },
    // 确认按钮操作
    handleConfirm() {
      if (this.ids.length === 1) {
        console.log("选中的人员信息:", this.form);
        // 向父组件传递选中的人员信息
        this.$emit("select-user", this.form.id, this.form.name, this.form.phone, this.form.position);
        this.dialogVisible = false;
      } else {
        this.$message.warning('请选择一个人员');
      }
    },
    // 禁止多选
    checkSelectable(row, selected) {
      return selected ? this.ids.length === 0 : true;
    },
    enterprise() {
      this.openUser = true;
    },
    close() {
      this.openUser = false
    },
    selectEnterprise(data) {
      this.form.enterpriseId = data.enterpriseId
      this.form.enterpriseName = data.enterpriseName
      console.log(this.form)
      this.close()
    },

    //序号展示
    indexMethod(index) {
      return (this.queryParams.pageNum - 1) * this.queryParams.pageSize + index + 1;
    },

    handlePictureCardPreview(id) {
      this.uploadId = id;
      this.openFileList = true;
    },

    handleRowClick(row) {
      // 单选逻辑，清空之前的选择
      this.ids = [row.id];
      this.form = { ...row };
      // 需要手动设置该行选中状态，因为使用了radio模式
      this.$refs.singleTable.clearSelection();
      this.$refs.singleTable.toggleRowSelection(row, true);
      
      // 如果用户点击确定按钮后，可以直接使用这个选中的数据
      this.single = false;
      this.multiple = false;
    },

  }
};
</script>
<style scoped>
.dialog-footer.centered-footer {
  text-align: center;
  margin-top: -10px;
  /* Adjust this value as needed */
}
</style>
